<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :before-open="beforeOpen"
      ref="crud"
      :permission="permissionList"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain @click="handleDelete" v-if="permission.solderPaste_delete">删 除 </el-button>
        <el-button type="warning" size="small" plain icon="el-icon-download" @click="handleExport">导出 </el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-excel
      :visible="exprotVisible"
      :option="option"
      :page="page"
      :data="exportData"
      name="锡膏物料导出"
      :loading="btnLoading"
      @selectData="handleSelectData"
      @close="handleClose"
    ></export-excel>
  </basic-container>
</template>
<script>
import { getList, getDetail, update, add, remove } from '@/api/equipment-manage/solder-paste-manage.js';
import exportExcel from '@/components/export-excel';
import { mapGetters } from 'vuex';
import { getSteelMeshList } from '@/api/admin/equipment';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    exportExcel,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'SOLDER_PASTE_MANAGE',
      exprotVisible: false,
      btnLoading: false,
      exportData: [],
      fileList: [],
      form: {},
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        editBtn: true,
        viewBtnText: '详情',
        viewTitle: '详情',
        delBtn: false,
        menu: true,
        selection: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 4,
        searchSpan: 5,
        menuWidth: 200, //弹框内字段宽度属性
        //   dialogWidth: 880, //弹框宽度属性
        labelWidth: '180',
        //   searchMenuSpan: 7,
        dialogClickModal: false,
        addBtnText: '添加物料编码',
        columnBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'code',
            search: true,
            rules: [
              {
                required: true,
                message: '请输入物料编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '保质期天数(天)',
            prop: 'expirationDays',
            formslot: true,
            type: 'number',
            precision: 0,
            controls: false,
            labelTip: '指锡膏可使用的时间管控，从锡膏生产日期开始计算过期时间；到达保质期后，对应的锡膏条码将自动报废',
            rules: [
              {
                required: true,
                message: '请输入保质期天数',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最长使用时间',
            prop: 'maxUsageTime',
            labelTip: '指从锡膏回温开始，到生产辅料登记的时间间隔，超过最长使用时间，将无法登记生产。'
          },
          {
            label: '最短回温时间(时)',
            prop: 'timeMinRise',
            type: 'number',
            precision: 2,
            controls: false,
            labelTip: '回温开始后，经过设定的时间后，自动视为回温结束，锡膏可进行搅拌',
            rules: [
              {
                required: true,
                message: '请输入最短回温时间',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最短搅拌时间(分)',
            prop: 'timeMinStir',
            type: 'number',
            precision: 2,
            controls: false,
            labelTip: '锡膏回温结束后，手动点击搅拌，记为搅拌开始，经过设定的时间后，自动视为搅拌完成，锡膏可领用',
            rules: [
              {
                required: true,
                message: '请输入最短搅拌时间',
                trigger: 'blur'
              }
            ]
          },
          // {
          //   label: '到达回温时间预警(时)',
          //   prop: 'timeRiseWarning',
          //   labelTip: '指达到回温结束时间前，提前进行预警提示；（该时间需小于最短回温时间）',
          //   hide: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: '请输入到达回温时间预警',
          //       trigger: 'blur'
          //     }
          //   ]
          // },
          {
            label: '回柜二次使用间隔(时)',
            prop: 'intervalNextUse',
            type: 'number',
            precision: 2,
            controls: false,
            labelTip: '指锡膏归还到下次领用的时间间隔',
            hide: true
          },
          {
            label: '最大使用次数(次)',
            prop: 'maxUsage',
            type: 'number',
            precision: 0,
            minRows: 1,
            controls: false,
            hide: true,
            labelTip: '指锡膏最多可以领用的次数，达到最大使用次数后，锡膏条码将自动报废，不可回柜'
          }
        ]
      },
      modalName: '', //打开公用模态框名称
      tableOption: {}, //公用模态框配置信息

      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.solderPaste_view, false),
        editBtn: this.vaildData(this.permission.solderPaste_edit, false),
        addBtn: this.vaildData(this.permission.solderPaste_add, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      params.type = 'PART';
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.exportData = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleExport() {
      this.exportData = this.data;
      this.exprotVisible = true;
    },
    handleClose(val) {
      this.exprotVisible = false;
      this.data = this.exportData.splice(0, this.page.pageSize);
    },
    handleSelectData(val) {
      let current = 1;
      let pageSize = this.page.pageSize;
      if (val === 'all') {
        // 接口返回上限500条数据
        pageSize = 500;
      } else {
        current = this.page.currentPage;
      }
      this.btnLoading = true;
      getList(current, pageSize, Object.assign({ type: 'PART', ...this.query })).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.exportData = data.records;
        this.btnLoading = false;
      });
    },
    addBtn() {},
    rowUpdate(form, index, done, loading) {
      update(form).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove([row.id]);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    }
  }
};
</script>
